package com.yunfei.schedule;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yunfei.api.buy.param.CheXiaoScheduleParam;
import com.yunfei.bo.*;
import com.yunfei.contants.ApplicationConstant;
import com.yunfei.contants.CheXiaoScheduleContants;
import com.yunfei.contants.FundFeneSureBookContants;
import com.yunfei.entity.LogContractGetEntity;
import com.yunfei.entity.TCustBuyinEntity;
import com.yunfei.entity.TSpciProdEntity;
import com.yunfei.entity.TblobstorageEntity;
import com.yunfei.mapper.crm.TblobtSorageMapper;
import com.yunfei.mapper.ods.TCustBuyinMapper;
import com.yunfei.mapper.ods.TLogContractGetMapper;
import com.yunfei.mapper.ods.TSpciProdMapper;
import com.yunfei.fegin.SchedualServiceHi;
import com.yunfei.procedure.ProductCheXiaoProcedure;
import com.yunfei.service.RedisService;
import com.yunfei.utils.ComponentFactory;

import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ScheduledTask {
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ScheduledTask.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
    /**
     * 每隔5秒执行, 单位：ms。
     */
   @Scheduled(fixedRate = 1000)
    public void testMyBatis() {
        try {
            RedisService redisService = ComponentFactory.getBean(RedisService.class);
            Environment env = ComponentFactory.getBean(Environment.class);
            List<CheXiaoScheduleParam> scheduleList=null;
            List<CheXiaoScheduleParam> scheduleTempList=null;
            if(redisService.exists(CheXiaoScheduleContants.CHEXIAO_SCHEDULE_KEY)){
                scheduleList= (List<CheXiaoScheduleParam>) redisService.get(CheXiaoScheduleContants.CHEXIAO_SCHEDULE_KEY);
            }
            //开关-存储过程是否执行完毕的标识
            boolean flag=true;
            if(redisService.exists(CheXiaoScheduleContants.IS_COMPLETE_KEY)){
                flag= (boolean) redisService.get(CheXiaoScheduleContants.IS_COMPLETE_KEY);
            }
            if(flag){
                //开始执行任务
                redisService.set(CheXiaoScheduleContants.IS_COMPLETE_KEY,false);
                if (scheduleList != null) {
                    int size = scheduleList.size();
                    if(size>0){
                        CheXiaoScheduleParam cheXiaoScheduleParam=scheduleList.get(0);
                        String reserveno=cheXiaoScheduleParam.getReserveno();
                        String projId=cheXiaoScheduleParam.getProjId();
                        //开始调用存储过程
                        long startTime = System.currentTimeMillis();    //获取开始时间
                        System.out.println("调用存储过程开始时间=========================="+dateFormat.format(new Date()));
                        String jdbcUrl=env.getProperty(ApplicationConstant.CRM_JDBC_URL);
                        String driveClassName=env.getProperty(ApplicationConstant.CRM_driverClassName);
                        String jdbcUserName=env.getProperty(ApplicationConstant.CRM_USERNAME);
                        String jdbcPassword=env.getProperty(ApplicationConstant.CRM_PASSWORD);
                        ProductCheXiaoProcedure productCheXiaoProcedure=new ProductCheXiaoProcedure();
                        productCheXiaoProcedure.callProcedure(jdbcUrl,driveClassName,jdbcUserName,jdbcPassword,reserveno,projId);
                        long endTime = System.currentTimeMillis();    //获取结束时间
                        System.out.println("调用存储过程结束时间=========================="+dateFormat.format(new Date()));
                        System.out.println("调用存储过程运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
                    }
                    if (size > 1) {
                        scheduleTempList = new ArrayList<>();
                        for (int i = 1; i < size; i++) {
                            scheduleTempList.add(scheduleList.get(i));
                        }
                    }
                    //更新缓存
                    redisService.remove(CheXiaoScheduleContants.CHEXIAO_SCHEDULE_KEY);
                    //重新添加缓存
                    if(null!=scheduleTempList&&scheduleTempList.size()>0){
                        redisService.set(CheXiaoScheduleContants.IS_COMPLETE_KEY,scheduleTempList);
                    }
                   //将存储过程的执行标识更改为执行完毕
                    redisService.set(CheXiaoScheduleContants.IS_COMPLETE_KEY,true);

                } else {
                    redisService.set(CheXiaoScheduleContants.IS_COMPLETE_KEY,true);
                    //System.out.println("我什么事都不用做，等待下次再说吧..................");
                }
            }else{
                System.out.println("上次的存储过程还没有执行完毕，请耐心等待..............");
                if(null!=scheduleList&&scheduleList.size()>0){
                    System.out.println("目前还有"+scheduleList.size()+"个存储过程需要执行.............................");
                }
                redisService.set(CheXiaoScheduleContants.IS_COMPLETE_KEY,true);
            }

        } catch (Exception ex) {
            System.out.println("撤销操作执行的存储过程出现异常，请联系孙总，查看存错过程pkg_yl_his_data.sp_autoReserveArrange_yl");
        } finally {

        }
    }
}

